package top.vanchor.myblog2back.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.vanchor.myblog2back.entity.BlogDealRecord;
import top.vanchor.myblog2back.entity.BlogReport;
import top.vanchor.myblog2back.service.*;
import top.vanchor.myblog2back.utils.JWTUtils;
import top.vanchor.myblog2back.utils.JwtInterceptor;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2022/2/18 12:47
 */
@RestController
@RequestMapping("/admin/feedBackReport")
public class FeedBackReportController {

    @Resource
    private IBlogReportService service;

    @Resource
    private IBlogDealRecordService dealRecordService;

    @Resource
    private IBlogUserService blogUserService;

    @Resource
    private IBlogBlogService blogBlogService;

    @Resource
    private IBlogCommentService blogCommentService;
    /**
     * 获取举报信息
     * @param pageNo
     * @param pageSize
     * @param type
     * @param status
     * @return
     */
    @GetMapping("/getList")
    @ResponseBody
    public JSONObject getList(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("type") Integer type,
            @RequestParam("status") Integer status
    ){
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonTemp = new JSONObject();
        PageInfo<BlogReport> list = new PageInfo<>();
        try {
            list = service.getList(pageNo,pageSize,type,status);
            jsonObject.put("code",200);
            jsonObject.put("msg","获取成功");
            jsonTemp.put("list",list.getList());
            jsonTemp.put("total",list.getTotal());
            jsonObject.put("data",jsonTemp);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，请求失败");
        }
        return jsonObject;
    }


    @ApiOperation("管理员处理举报内容")
    @PostMapping("/doEdit")
    @ResponseBody
    public JSONObject doEdit(@RequestBody JSONObject data){
        JSONObject jsonObject = new JSONObject();
        boolean isPass = Boolean.parseBoolean(String.valueOf(data.get("code")));
        Integer reportId = Integer.parseInt(String.valueOf(data.get("reportId")));
        String resultMsg = String.valueOf(data.get("resultMsg"));
        int banDay = "".equals(String.valueOf(data.get("banDay")))?3:Integer.parseInt(String.valueOf(data.get("banDay")));
        String updateTime = String.valueOf(System.currentTimeMillis()/1000);
        //获取需要受理的entity
        BlogDealRecord dealRecord = new BlogDealRecord();
        try {
            BlogReport report = service.getReportById(reportId);
            dealRecord = prepareBean(report,resultMsg,isPass,updateTime);
            //后台查重，判断是否已经受理过了
            if (dealRecordService.getCounts(dealRecord) > 0){
                jsonObject.put("code",510);
                jsonObject.put("msg","该请求已被处理，请勿重复操作！");
                return jsonObject;
            }
            if (dealRecordService.toAddItem(dealRecord) > 0){
                int ret = 0;
                int targetUserId = report.getTargetUserId();
                report.setReportFinishTime(updateTime);
                service.toDeal(report,isPass?1:2);
                if (isPass){
                    ret += blogUserService.changeStatusByUser(blogUserService.getById(targetUserId),banDay);
                    if (ret == -1){
                        dealRecord.setResultMsg(dealRecord.getResultMsg()+"(封禁失败，因为账号"+report.getTargetUserPhone()+"已被封禁，无法叠加封禁效果)");
                        jsonObject.put("msg","受理完成，但因用户正处于封禁状态，处罚不生效！");
                    }else{
                        dealRecord.setResultMsg(dealRecord.getResultMsg()+"(用户"+report.getTargetUserPhone()+"已被封禁"+banDay+"日)");
                        jsonObject.put("msg","受理成功！用户已被封禁"+banDay+"日！");
                    }
                    dealRecord.setBanDay(banDay);
                    dealRecordService.upDateInfo(dealRecord);
                }else{
                    jsonObject.put("code",200);
                    jsonObject.put("msg","受理完成，已驳回举报申请");
                }
            }else{
                jsonObject.put("code",500);
                jsonObject.put("msg","服务器异常，请求失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，请求失败");
        }
        return jsonObject;
    }
    public BlogDealRecord prepareBean(BlogReport report,String resultMsg,boolean isPass,String updateTime){
        BlogDealRecord dealRecord = new BlogDealRecord();
        dealRecord.setDealType(0);
        dealRecord.setReportType(report.getReportType());
        dealRecord.setHandleUserId(report.getHandleUserId());
        dealRecord.setTargetId(report.getTargetId());
        dealRecord.setAskReason(report.getReportReason());
        dealRecord.setFinishTime(updateTime);
        dealRecord.setHandleTime(report.getReportHandleTime());
        dealRecord.setResultMsg(resultMsg);
        dealRecord.setAdminId(Integer.parseInt(Objects.requireNonNull(JWTUtils.getUserId(JwtInterceptor.getTokenInfo()))));
        dealRecord.setTargetBody(report.getTargetBody());
        dealRecord.setResultStatus(isPass?1:2);
        return dealRecord;
    }

}
